package GroupingComparator;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderBean implements WritableComparable<OrderBean> {
    // 订单id号
    private int order_id;
    // 价格
    private double price;

    @Override
    public String toString() {
        return order_id + "\t" + price;
    }

    @Override
    public int compareTo(OrderBean o) {

        int result;

        if (this.order_id > o.getOrder_id()) {
            result = 1;
        } else if (this.order_id < o.getOrder_id()) {
            result = -1;
        } else {
            //id正序，价格倒序
            result = 0;
//                    this.price > o.getPrice() ? -1 : 1;
        }
        return result;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(order_id);
        out.writeDouble(price);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.order_id = in.readInt();
        this.price = in.readDouble();
    }
}
